package pro.resources;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * @author  sangwo.y
 */
public class ResourceLocator {
	
	private static ResourceLocator instance;
	
	private ResourceLocator() {
	}

	/**
	 * @return  the instance
	 * @uml.property  name="instance"
	 */
	public static ResourceLocator getInstance() {
		if (instance == null)
			instance = new ResourceLocator();
		return instance;
	}
	
	public ImageIcon getIcon(String iconName){
        java.net.URL imgURL = instance.getClass().getResource("pics/"+iconName+".png");
        if (imgURL != null) {
            return new ImageIcon(imgURL, iconName);
        } else {
            System.err.println("Couldn't find file: " + iconName);
            return null;
        }
	}
	
	public BufferedImage getBufferedImage(String imgName) {
		InputStream imageURL = instance.getClass().getResourceAsStream("pics/"+imgName);
		try {
			BufferedImage image = ImageIO.read(imageURL);
			return image;
		} catch (Exception e) {return null;}
	}
	
	public Image getImage(String imgName) {
		InputStream imageURL = instance.getClass().getResourceAsStream("pics/"+imgName);
		try {
			Image image = ImageIO.read(imageURL);
			return image;
		} catch (Exception e) {return null;}
	}
	
	public File getFile(String fileName) {
		try {
			return new File(instance.getClass().getResource("files/"+fileName).getFile());
			
		} catch (Exception e) {return null;}
		
		
	}
	
	public InputStream getFileAsInputStream(String fileName) {
		InputStream is = instance.getClass().getResourceAsStream("files/"+fileName);
		return is;
	}
} 
